(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.createVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'resourceBundleService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, resourceBundleService) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.resourceBundle = {};
		ctrl.resourceBundleOld = {};

		ctrl.init = function() {
			authService.initialOption().then(function(response) {
				ctrl.localOptions = response.data.localList;
				ctrl.refBundleOptions = response.data.bundleList;
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response;
			});
			ctrl.resourceBundle = resourceBundleInfo;
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, null, sortChanged);
			ctrl.gridOptions.enableRowHeaderSelection = false;
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			resourceBundleInfo.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			authService.createVerify(resourceBundleInfo).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.resourceBundle = response.data.model;
					angular.merge(ctrl.searchCriteria, response.data.model.searchCriteria);
					gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
					ctrl.resourceBundle.taskId = response.data.task.taskId;
				}
			});
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.resourceBundle.comments = comments;
						authService.createVerifyReject(ctrl.resourceBundle).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.resourceBundle.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.resourceBundle).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundle.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundle.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);